home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr13 / aurora2c.zip / EXAMPLE.AML < prev    next >
Text File  |  1995-04-07  |  20KB  |  692 lines

  1.  
  2. // ───────────────────────────────────────────────────────────────────
  3. // The Aurora Editor v2.0
  4. // Copyright 1993-1995 nuText Systems. All Rights Reserved Worldwide.
  5. //
  6. // AML examples and code fragments
  7. // ───────────────────────────────────────────────────────────────────
  8.  
  9.  
  10. // Example 1 --------------------------------------------------
  11.  
  12. // mark the entire file
  13. // (assigned to <f12> key - place in the "edit" object)
  14.  
  15.   key  <f12>
  16.     markline 1 (getlines)
  17.   end
  18.  
  19. // Example 2 --------------------------------------------------
  20.  
  21. // Demonstrates block marking without having the cursor resize the mark
  22. // (for compatability with the old 'DrawMark' config setting in v1.2)
  23.  
  24.   key  <alt l>
  25.                 markline
  26.                 stopmark    // stopmark closes the mark
  27.   key  <alt a>
  28.                 markchar
  29.                 stopmark
  30.   key  <alt b>
  31.                 markcolumn
  32.                 stopmark
  33.  
  34.  
  35. // Example 3 --------------------------------------------------
  36.  
  37. // block copy without moving the mark (provided for compatibility
  38. // with the old 'MoveMark' config setting in v1.2)
  39.  
  40.   // block copy
  41.   key  <alt c>
  42.     undobegin                // group together as one undoable operation
  43.     curr_mark = getmarkuse   // get the default markid
  44.     copymark curr_mark 'T'   // create temp copy of original mark
  45.     usemark 'T'              // use the temp mark
  46.     copyblock2               // copy marked text (moves the mark also)
  47.     destroymark              // destroy the temp mark
  48.     usemark curr_mark        // switch back to the original mark
  49.     undoend
  50.   end
  51.  
  52.  
  53. // Example 4 --------------------------------------------------
  54.  
  55. // Automark example - creates a temporary paragraph mark if
  56. // no mark exists (provided for compatibility with the old
  57. // AutoMark configuration setting in v1.2)
  58.  
  59.   // begin automark
  60.   function  begauto
  61.     if not mark? then        // if no mark exists...
  62.       set _am ON             // set a flag
  63.       markpara               // mark the current paragraph
  64.     end
  65.   end
  66.  
  67.   // end automark
  68.   function  endauto
  69.     if _am then              // if flag was set..
  70.       set _am OFF            // unset flag
  71.       destroymark            // destroy the temporary mark
  72.     end
  73.   end
  74.  
  75.   // usage example (indent a block)
  76.   key  <shift f8>
  77.     begauto
  78.     shiftblock 1
  79.     endauto
  80.   end
  81.  
  82.  
  83. // Example 5 --------------------------------------------------
  84.  
  85. // define <home> and <end> key behaviour to match the the old
  86. // RepEnd config setting in v1.2 (provided for compatibility
  87. // with v1.2)
  88.  
  89.   // <home> key
  90.   key  <home>
  91.     if getcol == 1 then             // at col 1?
  92.       up                            // then move up 1 line
  93.     else
  94.       col 1                         // otherwise move to column 1
  95.     end
  96.   end
  97.  
  98.   // <end> key
  99.   key  <end>
  100.     end_of_line = getlinelen + 1    // get column after end-of-line
  101.     if getcol == end_of_line then   // are we there already?
  102.       down                          // move down 1 line
  103.       col  getlinelen + 1           // move to end-of-line on new line
  104.     else
  105.       col end_of_line               // move to end-of-line
  106.     end
  107.   end
  108.  
  109.  
  110. // Example 6 --------------------------------------------------
  111.  
  112. // Find the last string with wrap around (provided for compatibility
  113. // with the old 'SearchWrap' config setting in v1.2)
  114.  
  115.   function findlastw
  116.  
  117.     var  search_str
  118.     var  options
  119.  
  120.     // get last find string
  121.     history_str = gethiststr "_find"
  122.  
  123.     // do the search. If not found...
  124.     if not search2 history_str '' TRUE then
  125.  
  126.       // wrap to top or bottom
  127.       if (splitstr '' history_str ref search_str ref options ) >= 2 and
  128.          (pos 'r' options) then
  129.         row (getlines)
  130.         col getlinelen + 1
  131.       else
  132.         gotopos 1 1
  133.       end
  134.  
  135.       // do the search again
  136.       search2 history_str
  137.     end
  138.   end
  139.  
  140.   // usage example
  141.   key <ctrl l>  findlastw
  142.  
  143.  
  144. // Example 7 --------------------------------------------------
  145.  
  146. // Change the left and right cursor keys to wrap to the previous or
  147. // next line
  148.  
  149.   // cursorleft with wrap
  150.   key  <left>
  151.     if getcol == 1 then             // wrap if at column 1
  152.       if up then                    // ..and not at first line
  153.         col getlinelen + 1
  154.       end
  155.     else
  156.       left
  157.     end
  158.   end
  159.  
  160.   // cursorright with wrap
  161.   key  <right>
  162.     if getcol > getlinelen then     // wrap if at end-of-line
  163.       if down then                  // ..and not at last line
  164.         col 1
  165.       end
  166.     else
  167.       right
  168.     end
  169.   end
  170.  
  171.  
  172.  
  173. // Example 8 --------------------------------------------------
  174.  
  175. // change the <del> key to delete a block if marked,
  176. // otherwise delete a character
  177.  
  178.   key  <del>
  179.     if mark? then
  180.       deleteblock                      // delete block if it exists,
  181.     else
  182.       delchar2                         // otherwise delete a character
  183.     end
  184.   end
  185.  
  186.  
  187. // Example 9 --------------------------------------------------
  188.  
  189. // find the word at the cursor
  190. // (assigned to <ctrl f11>)
  191.  
  192.   key  <ctrl f11>
  193.     wordstr = getword           // get word using the default charset
  194.     if wordstr then
  195.       search2 wordstr           // find the word
  196.     else
  197.       say "no word at the cursor" 'b'
  198.     end
  199.   end
  200.  
  201.  
  202. // Example 10 -------------------------------------------------
  203.  
  204. // insert a ruler line above the current line
  205. // (assigned to <alt k> key)
  206.  
  207.   key  <alt k>
  208.     insabove "····+····1····+····2····+····3····+····4····+····5····+····6····+····7····+····8"
  209.   end
  210.  
  211.  
  212. // Example 11 -------------------------------------------------
  213.  
  214. // emulate the QEdit 'GetPrev' <ctrl -> command:  copies characters
  215. // from the line above the current line (assigned to <alt 4> key)
  216.  
  217.   key  <alt 4>
  218.     if getrow > 1 then                         // if after first line...
  219.       writetext (getchar (getcol) getrow - 1)  // get char and write it
  220.     end
  221.   end
  222.  
  223.  
  224. // Example 12 -------------------------------------------------
  225.  
  226. // Emulation of Brief-style <home> and <end> keys. These keys should
  227. // be placed in the 'edit' object:
  228.  
  229.   // brief <home> key emulation (br)
  230.   key <home>
  231.     col 1                                      // goto column 1
  232.     smark                                      // cua marking
  233.     keycode = getkey                           // get next key
  234.     if keycode == <home> then                  // <home> pressed 2nd time?
  235.       row (getviewtop)                         // goto to page top
  236.       smark                                    // cua marking
  237.       keycode = getkey                         // get next key
  238.       if keycode == <home> then                // <home> pressed 3rd time?
  239.         row 1                                  // goto to top of file
  240.         smark                                  // cua marking
  241.       else
  242.         queuekey keycode                       // execute key normally
  243.       end
  244.     else
  245.       queuekey keycode                         // execute key normally
  246.     end
  247.   end
  248.  
  249.   // brief <end> key emulation (br)
  250.   key <end>
  251.     col  getlinelen + 1                        // goto end-of-line
  252.     smark                                      // cua marking
  253.     keycode = getkey                           // get next key
  254.     if keycode == <end> then                   // <end> pressed 2nd time?
  255.       row (getviewbot)                         // goto to page bottom
  256.       smark                                    // cua marking
  257.       keycode = getkey                         // get next key
  258.       if keycode == <end> then                 // <end> pressed 3rd time?
  259.         row (getlines)                         // goto to bottom of file
  260.         smark                                  // cua marking
  261.       else
  262.         queuekey keycode                       // execute key normally
  263.       end
  264.     else
  265.       queuekey keycode                         // execute key normally
  266.     end
  267.   end
  268.  
  269.  
  270. // Example 13 -------------------------------------------------
  271.  
  272. // shows how to 'double-up' key usage for some keys by testing
  273. // for the <shift> key
  274.  
  275.   key  <alt k>
  276.     if shiftkey? then             // <alt shift k>
  277.       .
  278.       .
  279.     else                          // <alt k>
  280.       .
  281.       .
  282.     end
  283.   end
  284.  
  285.  
  286. // Example 14 -------------------------------------------------
  287.  
  288. // prompts the user for the name of a program and passes the word
  289. // at the cursor to it as the first parameter
  290.  
  291.   key  <shift f12>
  292.     parm = getword _CSetB                  // get word using charset CSetB
  293.     if parm then
  294.       program = ask "Program to execute"   // prompt user for program
  295.       if program then                      // program entered?
  296.         run  program + ' ' + parm  "ck"    // concatenate program with word
  297.       end                                  //   and execute it
  298.     else
  299.       say "no word at the cursor" 'b'      // no word found at the cursor
  300.     end
  301.   end
  302.  
  303.  
  304. // Example 15 -------------------------------------------------
  305.  
  306. // strip leading or trailing spaces from a string
  307.  
  308.   // return string 'charstring' without leading spaces
  309.   function  stripl (charstring)
  310.     return  charstring [posnot ' ' charstring : 0]
  311.   end
  312.  
  313.   // return string 'charstring' without trailing spaces
  314.   function  stript (charstring)
  315.     return  charstring [1 : posnot ' ' charstring 'r']
  316.   end
  317.  
  318.  
  319. // Example 16 -------------------------------------------------
  320.  
  321. // attach "properties" to a string value in the current object
  322. // (demonstrates the use of setx, lookup, unsetx)
  323.  
  324.   // set a property
  325.   function  setprop (string prop value)
  326.     setx  string + '-' + prop  value
  327.   end
  328.  
  329.   // get a property
  330.   function  getprop (string prop)
  331.     lookup  string + '-' + prop
  332.   end
  333.  
  334.   // delete a property
  335.   function  delprop (string prop)
  336.     unsetx  string + '-' + prop
  337.   end
  338.  
  339.  
  340. // Example 17 -------------------------------------------------
  341.  
  342. // modify the save-as prompt to change the file name
  343. // (replace function "asksaveas" in EXT.AML)
  344.  
  345.   function  asksaveas (options)
  346.     file = ask "Save " + (getname (getbufname)) + " as"  "_load"
  347.     if file then
  348.       file = qualify file (getbufname)
  349.       addhistory "_load" file
  350.       save file options   // save file with name "file"
  351.       setname file        // name current file "file"
  352.     end
  353.   end
  354.  
  355.  
  356. // Example 18 -------------------------------------------------
  357.  
  358. // Compute the factorial of a number (illustrates AML recursion)
  359.   function  fact (x)
  360.     if? x <= 1  1  x * (fact x - 1)
  361.   end
  362.  
  363.   // display the factorial of 6
  364.   say (fact 6)
  365.  
  366.  
  367. // Example 19 -------------------------------------------------
  368.  
  369. // generate a random number between a and b
  370. // (demonstrates the use of the rand function)
  371.  
  372.   function  random (a b)
  373.     a + (rand mod b - a + 1)
  374.   end
  375.  
  376.   // display random number between 500 and 3000
  377.   say (random 500 3000)
  378.  
  379.  
  380. // Example 20 -------------------------------------------------
  381.  
  382. // asynchronous beep
  383. // (set a timer to stop the beep - demonstrates the use of timers)
  384.  
  385.   function  beep2 (freq duration)
  386.     if not arg then
  387.       // stop the beep
  388.       beep
  389.     else
  390.       // beep indefinitely
  391.       beep freq
  392.       // call this function again after 'duration' milliseconds
  393.       settimer "beep" duration '' "beep2"
  394.     end
  395.   end
  396.  
  397.  
  398. // Example 21 -------------------------------------------------
  399.  
  400. // Check newly open files for tab chars. Expand tabs if found.
  401.  
  402.   // (Place this section of aml code at the end of the 'onopen'
  403.   // function in the 'edit' object in EXT.AML - The 'onopen' function
  404.   // is called after a file is loaded and before it is displayed)
  405.  
  406.   if not getbinarylen then        // only for non-binary files
  407.     markline 1 20                 // create a mark (first 20 lines)
  408.     if find (char 9) "bgn*" then  // search for tab char in the mark
  409.       markline 1 (getlines)       // extend the mark to end-of-file
  410.       tabblock _TabWidth          // expand tabs using TabWidth
  411.       bufferflag '-m'             // turn off buffer-modified flag
  412.     end
  413.     destroymark                   // destroy the temporary mark
  414.   end
  415.  
  416.  
  417. // Example 22 -------------------------------------------------
  418.  
  419. // toggle the case of the character at the cursor
  420. // (assigned to <alt k>)
  421.  
  422.   key <alt k>
  423.     ovltext (flipcase (getchar))     // toggle the character
  424.     right                            // move right one column
  425.   end
  426.  
  427.  
  428. // Example 23 -------------------------------------------------
  429.  
  430. // transpose the characters at the cursor
  431. // (assigned to <alt k>)
  432.  
  433.    // method 1: using a marked block
  434.    key <alt k>
  435.      undobegin        // beginning of an undoable group
  436.      usemark 'T'      // use temporary mark 'T'
  437.      markchar         // create a character mark
  438.      stopmark         // stop the cursor from extending the mark
  439.      right 2          // move right two columns
  440.      moveblock        // move the marked character
  441.      destroymark      // destroy the mark
  442.      usemark          // switch back to mark '*'
  443.      undoend          // end of an undoable group
  444.    end
  445.  
  446.   // method 2: using a string
  447.   key <alt k>
  448.     twochars = gettext (getcol) 2
  449.     ovltext (if? twochars [2] twochars [2] ' ') + twochars [1]
  450.     right
  451.   end
  452.  
  453.  
  454. // Example 24 -------------------------------------------------
  455.  
  456. // macro code fragment to synchronously dispatch all the events
  457. // currently in the event queue
  458.  
  459.   endprocess          // queue an internal 'queue quit' event
  460.   process             // process all events and return
  461.  
  462.  
  463. // Example 25 -------------------------------------------------
  464.  
  465. // macro code fragment to display a timed message box
  466.  
  467.   // set a timer (t1) to simulate the <esc> key one second from now
  468.   settimer "t1" 1000 '' <esc>
  469.  
  470.   // display a short-style message box
  471.   shortbox "timed message"
  472.  
  473.  
  474. // Example 26 -------------------------------------------------
  475.  
  476. // modify the prompt history menu to automatically enter selected
  477. // strings into the prompt
  478.  
  479.  
  480.   // replace the <pgup> and <pgdn> keys in the 'prompt' object
  481.   // in KBD.AML
  482.  
  483.   key  <pgup>
  484.     if askhistory then
  485.       queue <enter>
  486.     end
  487.   end
  488.  
  489.   key  <pgdn>  call <pgup>
  490.  
  491.   // ..also modify the <lbutton> function in the 'prompt' object
  492.   // in MOUSE.AML to handle mouse clicks on the prompt retrieve tab
  493.  
  494.   // left button down
  495.   function  <lbutton>
  496.      .
  497.      .
  498.     case getregion
  499.  
  500.       // modify this 'when' clause
  501.       when 14
  502.         if askhistory then
  503.           queue <enter>
  504.         end
  505.          .
  506.          .
  507.  
  508. // Example 27 -------------------------------------------------
  509.  
  510. // The following is an example of an external macro which can
  511. // be run from the command line (without actually starting the
  512. // editor). This macro replaces all occurrences of a search
  513. // string in a file:
  514.  
  515.   if loadbuf (arg 2) then                  // load a file
  516.     replace (arg 3) (arg 4) (arg 5) + 'a*' // replace all string(s)
  517.     savebuf (arg 2)                        // save the file
  518.   end
  519.  
  520. // assuming this macro was compiled to 'replace.x', the following
  521. // command will run the macro from the DOS command line:
  522.  
  523.   C>a -xreplace.x "myfile.txt" "apples" "oranges" 'i'
  524.          // replaces all occurrences of 'apples' with 'oranges',
  525.          // in 'myfile.txt', ignoring case
  526.  
  527.  
  528. // Example 28 -------------------------------------------------
  529.  
  530. // The following example shows how to locate a file in a path
  531. // defined by a DOS environment string:
  532.  
  533.    file = locatefile "program.exe" (getenv 'PATH')
  534.      // 'PATH' must be in caps
  535.  
  536.    msgbox "Filename is: " + file
  537.      // filename is fully qualified
  538.  
  539.  
  540. // Example 29 -------------------------------------------------
  541.  
  542. // To change the file manager to recognize unshifted characters as
  543. // command codes (like Aurora v1.2), modify the <char> key
  544. // definition in the fmgr object in KBD.AML:
  545.  
  546.   key  <char> (c)
  547.       .
  548.       .
  549.     // <shift-character> commands
  550.     elseif not shiftkey? then         // add 'not' to this line
  551.       .
  552.       .
  553.  
  554.   // (note that this will also change each file manager search
  555.   // 'hotkey' to <shift-hotkey>)
  556.  
  557.  
  558. // Example 30 -------------------------------------------------
  559.  
  560. // The following macro code shows how to put a simple real-time clock on
  561. // the edit window title bar:
  562.  
  563.   object edit
  564.      .
  565.     // called every second
  566.     function  onesec
  567.       settitle getbufname + ' ' + gettime
  568.     end
  569.      .
  570.      .
  571.   end
  572.  
  573.   // called when the editor is started
  574.   function  onentry
  575.       .
  576.     // set a timer to call 'onesec' every second
  577.     setrepeat 'sec' 1000 '' "onesec"
  578.       .
  579.       .
  580.   end
  581.  
  582.  
  583. // Example 31 -------------------------------------------------
  584.  
  585. // The following macro code shows how set an alarm to go off every
  586. // hour on the hour in edit windows and file manager windows
  587.  
  588.   object edit_fmgr
  589.      .
  590.     // called on the hour (Big Ben clock chime)
  591.     function  onhour
  592.       beep 330  600
  593.       beep 262  600
  594.       beep 294  600
  595.       beep 196 1200
  596.       beep 196  600
  597.       beep 294  600
  598.       beep 330  600
  599.       beep 262 1200
  600.     end
  601.      .
  602.      .
  603.   end
  604.  
  605.   // called when the editor is started
  606.   function  onentry
  607.      .
  608.     // set an alarm timer to call 'onhour' every hour on the hour
  609.     setalarm 'hr'
  610.        -1               // any year
  611.        -1               // any month
  612.        -1               // any day
  613.        -1               // any weekday
  614.        -1               // any hour
  615.         0               // only at zero minutes past the hour
  616.         0               // only at zero seconds past zero minutes
  617.        ''               // dispatch in the current event object
  618.        'onhour'         // execute the function 'onhour'
  619.      .
  620.      .
  621.   end
  622.  
  623.  
  624. // Example 32 -------------------------------------------------
  625.  
  626. // The following macro code shows how to create a persistent
  627. // real-time clock window which is updated every second:
  628.  
  629.   // create a new clock object descended from the 'win' object,
  630.   // so it can inherit the mouse handling and other capabilities
  631.   // of 'desktop' windows (like edit and file manager windows)
  632.   object  clock (win)
  633.  
  634.   // called every second to update the date and time
  635.   function tick
  636.     gotowindow "clock"
  637.     writestr getdate + ' ' + gettime  (color brightred on black)  4 2
  638.     gotowindow
  639.   end
  640.  
  641.   // destroy the timer if the clock window is closed
  642.   key <esc>
  643.     destroytimer "clk"
  644.     pass
  645.   end
  646.  
  647.   function "≡"
  648.     call <esc>
  649.   end
  650.  
  651.   // provide keyboard support for switching windows
  652.   key <ctrl a>
  653.     nextwindow
  654.   end
  655.  
  656.   // (be sure to change the object if unrelated code follows)
  657.   .
  658.   .
  659.  
  660.   object  edit
  661.  
  662.   // create the clock window and start the clock (this could actually
  663.   // be placed in any convenient object or function, or it could
  664.   // be an external macro)
  665.   key <alt k>
  666.     createwindow 'clock'
  667.     // route window events to the clock object above
  668.     setwinobj "clock"
  669.  
  670.     setframe ">b"
  671.     setwinctrl '≡'
  672.     setcolor  border_color   color white on gray
  673.     setcolor  text_color     color black on gray
  674.     settitle "Clock" 'c'
  675.     setborder "i1"
  676.     setshadow 2 1
  677.  
  678.     // center the window
  679.     width = (sizeof getdate + gettime) + 9
  680.     height = 4
  681.     ox = (getvidcols - width) / 2
  682.     oy = (getvidrows - height) / 2
  683.     sizewindow ox oy ox + width oy + height "ad"
  684.  
  685.     // simulate the first tick
  686.     sendobject "clock" "tick"
  687.     // start the clock timer
  688.     setrepeat 'clk' 1000 'clock' "tick"
  689.   end
  690.  
  691.  
  692.